package gui.eventListener;

import gui.DrawPanel;

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import javax.swing.SwingUtilities;

import dados.Ponto;
import desenhando.EstadoDesenhando;

public class Mouse implements MouseListener, MouseMotionListener {
	private DrawPanel drawPanel;
	private EstadoDesenhando draw;
	
	private Ponto selectedPoint;
	private boolean pegou;

	public Mouse(DrawPanel drawPanel) {
		this.draw = drawPanel.getDraw();
		this.drawPanel = drawPanel;
		this.selectedPoint = null;
		this.pegou = false;
	}

	@Override
	public void mouseClicked(MouseEvent e) {
		
		if(e.getX() < 820) {
			// Subtraindo as coordenadas X e Y para melhor precisao do clique
			int x = e.getX() - 15;
			int y = e.getY() - 35;

			Ponto p = new Ponto(x, y);
			
			Ponto aux = null;
			boolean stopSearch = false;

			// Searching if have an existing point
			for (int i = 0; i < this.draw.getCurves().size() && !stopSearch; i++) {

				aux = this.draw.getCurves().get(i).precisionSquare(p);

				if(aux != null) {
					stopSearch = true;
					this.draw.setCurveIndex(i);
				}

			}
			
			LeftButton(e, p, aux);
			RightButton(e, p, aux);

			this.drawPanel.getMenu().preencherListaCurvas();
			this.drawPanel.update();

		}
	}

	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub
		this.pegou = false;
	}

	public Ponto getSelectedPoint() {
		return selectedPoint;
	}

	public void setSelectedPoint(Ponto selectedPoint) {
		this.selectedPoint = selectedPoint;
	}
	
	public void LeftButton(MouseEvent e, Ponto p, Ponto aux) {
		if(SwingUtilities.isLeftMouseButton(e)) {

			if (aux != null) {	// Clicou num ponto existente
				
				// Clicou em outro ponto existente e
				// Verificando se um ponto foi selecionado anteriormente
				if(!aux.isSelecionado()) {
					if(selectedPoint != null) {
						selectedPoint.setSelecionado(false);
					}
					aux.setSelecionado(true);
					this.draw.pointSelection(this.drawPanel.getGraphics(), aux);
				}
				
				selectedPoint = aux;
				
			} else { // Clicked on a "blank" part of the Panel
				if(selectedPoint != null){
					selectedPoint.setSelecionado(false);
					selectedPoint = null;
				} 
				

			}
			
		}
	}
	
	public void RightButton(MouseEvent e, Ponto p, Ponto aux) {
		if(SwingUtilities.isRightMouseButton(e)) {
			
			if(aux == null) {
				this.draw.addPoint(p);
			}
			
		}
	}

	@Override
	public void mouseDragged(MouseEvent e) {
		// TODO Auto-generated method stub
		if(SwingUtilities.isLeftMouseButton(e)) {

			int x = e.getX() - 15;
			int y = e.getY() - 35;

			int precision = 10;
			
			if(selectedPoint != null) {
			
				if(this.pegou) {
					selectedPoint.setX(x);
					selectedPoint.setY(y);
					this.draw.getCurves().get(this.draw.getCurveIndex()).setModified(true);
					this.drawPanel.getMenu().preencherListaCurvas();
					this.drawPanel.update();
				}
				else if (x <= selectedPoint.getX() + precision && x > selectedPoint.getX() - precision 
						&& y <= selectedPoint.getY() + precision && y > selectedPoint.getY() - precision) {
					this.pegou = true;
				}
				
				


			}

		}
		
	}

	@Override
	public void mouseMoved(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	
	
}
